package com.jindy.module.statement.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.jindy.common.core.domain.entity.SysUser;
import com.jindy.common.utils.DateUtils;
import com.jindy.common.utils.SecurityUtils;
import com.jindy.module.constant.StatusConstants;
import com.jindy.module.machine.domain.Machine;
import com.jindy.module.machine.domain.MachineRecord;
import com.jindy.module.machine.mapper.MachineRecordMapper;
import com.jindy.module.order.domain.Order;
import com.jindy.module.order.domain.OrderProduct;
import com.jindy.module.order.mapper.OrderMapper;
import com.jindy.module.order.mapper.OrderProductMapper;
import com.jindy.module.outboundorder.domain.OutboundOrderMachine;
import com.jindy.module.outboundorder.mapper.OutboundOrderMachineMapper;
import com.jindy.module.statement.domain.*;
import com.jindy.module.statement.mapper.*;
import com.jindy.module.statement.service.StatementExpensesService;
import com.jindy.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jindy.module.statement.service.IStatementService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 结算单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-27
 */
@Service
public class StatementServiceImpl implements IStatementService
{
    @Autowired
    private StatementMapper statementMapper;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private DeductibleExpensesMapper deductibleExpensesMapper;
    @Resource
    private LogisticsExpensesMapper logisticsExpensesMapper;
    @Resource
    private MaintenanceCostMapper maintenanceCostMapper;
    @Resource
    private OtherExpensesMapper otherExpensesMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderProductMapper orderProductMapper;
    @Resource
    private MachineRecordMapper machineRecordMapper;
    @Resource
    private OutboundOrderMachineMapper outboundOrderMachineMapper;
    @Resource
    private StatementExpensesMapper statementExpensesMapper;


    /**
     * 查询结算单
     *
     * @param id 结算单主键
     * @return 结算单
     */
    @Override
    public Statement selectStatementById(Long id)
    {
        Statement or = statementMapper.selectStatementById(id);
        setList(or);
        return or;
    }

    /**
     * 查询结算单列表
     *
     * @param statement 结算单
     * @return 结算单
     */
    @Override
    public List<Statement> selectStatementList(Statement statement)
    {

        List<Statement> list =  statementMapper.selectStatementList(statement);
        if(!CollectionUtils.isEmpty(list)){
            list.forEach(or->{
                setList(or);
            });
        }
        return list;
    }

    /**
     * 设置各个明细
     * @param or
     */
    void setList(Statement or){
        //项目应扣费用对象
        DeductibleExpenses deductibleExpenses = new DeductibleExpenses();
        deductibleExpenses.setOrderId(or.getId());
        deductibleExpenses.setOrderType("2");
        List<DeductibleExpenses> deductibleExpensesList = deductibleExpensesMapper.selectDeductibleExpensesList(deductibleExpenses);
        or.setDeductibleExpensesList(deductibleExpensesList);
        //物流及辅料费用对象
        LogisticsExpenses logisticsExpenses = new LogisticsExpenses();
        logisticsExpenses.setOrderId(or.getId());
        logisticsExpenses.setOrderType("2");
        List<LogisticsExpenses> logisticsExpensesList = logisticsExpensesMapper.selectLogisticsExpensesList(logisticsExpenses);
        or.setLogisticsExpensesList(logisticsExpensesList);

        MaintenanceCost maintenanceCost = new MaintenanceCost();
        maintenanceCost.setOrderId(or.getId());
        maintenanceCost.setOrderType("2");
        List<MaintenanceCost> maintenanceCostList = maintenanceCostMapper.selectMaintenanceCostList(maintenanceCost);
        or.setMaintenanceCostList(maintenanceCostList);

        OtherExpenses otherExpenses = new OtherExpenses();
        otherExpenses.setOrderId(or.getId());
        otherExpenses.setOrderType("2");
        List<OtherExpenses> otherExpensesList = otherExpensesMapper.selectOtherExpensesList(otherExpenses);
        or.setOtherExpensesList(otherExpensesList);

        //结算机器费用
        StatementExpenses statementExpenses = new StatementExpenses();
        statementExpenses.setOrderId(or.getOrderId());
        statementExpenses.setAccountStatementId(or.getId());
        statementExpenses.setOrderType("2");
        List<StatementExpenses> statementExpensesList = statementExpensesMapper.selectStatementExpensesList(statementExpenses);
        or.setStatementExpensesList(statementExpensesList);

    }

    /**
     * 新增结算单
     *
     * @param statement 结算单
     * @return 结果
     */
    @Override
    public int insertStatement(Statement statement)
    {

        SysUser sysUser = sysUserService.selectUserByUserName(SecurityUtils.getUsername());
        statement.setCreateBy(sysUser.getUserName());
        statement.setCreateTime(DateUtils.getNowDate());
        int result = statementMapper.insertStatement(statement);
        //项目应扣费用对象
        if(!CollectionUtils.isEmpty(statement.getDeductibleExpensesList())){
            statement.getDeductibleExpensesList().forEach(deductibleExpenses -> {
                deductibleExpenses.setOrderId(statement.getId());
                deductibleExpenses.setOrderType("2");
            });
            deductibleExpensesMapper.insertDeductibleExpensess(statement.getDeductibleExpensesList());
        }
        //物流及辅料费用对象
        if(!CollectionUtils.isEmpty(statement.getLogisticsExpensesList())){
            statement.getLogisticsExpensesList().forEach(logisticsExpenses -> {
                logisticsExpenses.setOrderId(statement.getId());
                logisticsExpenses.setOrderType("2");
            });
            logisticsExpensesMapper.insertLogisticsExpensess(statement.getLogisticsExpensesList());
        }
        // 维修、配件费用对象
        if(!CollectionUtils.isEmpty(statement.getMaintenanceCostList())){
            statement.getMaintenanceCostList().forEach(maintenanceCost -> {
                maintenanceCost.setOrderId(statement.getId());
                maintenanceCost.setOrderType("2");
            });
            maintenanceCostMapper.insertMaintenanceCosts(statement.getMaintenanceCostList());
        }
        //收取客户其他费用
        if(!CollectionUtils.isEmpty(statement.getOtherExpensesList())){
            statement.getOtherExpensesList().forEach(otherExpenses -> {
                otherExpenses.setOrderId(statement.getId());
                otherExpenses.setOrderType("2");
            });
            otherExpensesMapper.insertOtherExpensess(statement.getOtherExpensesList());
        }

        //结算机器费用
        if(!CollectionUtils.isEmpty(statement.getStatementExpensesList())){
            statement.getStatementExpensesList().forEach(statementExpenses -> {
                statementExpenses.setOrderId(statement.getOrderId());
                statementExpenses.setStatementId(statement.getId());
                statementExpenses.setOrderType("2");
                statementExpensesMapper.insertStatementExpenses(statementExpenses);
            });
//            statementExpensesMapper.insertStatementExpensess(statement.getStatementExpensesList());
        }

        //修改订单状态为结算结束
        Order order1 = orderMapper.selectOrderById(statement.getOrderId());
        order1.setOrderStatus(StatusConstants.OrderStatusConstant.orderStatus_6);
        orderMapper.updateOrder(order1);

        return result;
    }

    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    private int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            return day2 - day1;
        }
    }

    /**
     * 修改结算单
     *
     * @param statement 结算单
     * @return 结果
     */
    @Override
    public int updateStatement(Statement statement)
    {
        SysUser sysUser = sysUserService.selectUserByUserName(SecurityUtils.getUsername());
        statement.setUpdateBy(sysUser.getUserName());
        statement.setUpdateTime(DateUtils.getNowDate());
        //删除项目应扣费用对象
        deductibleExpensesMapper.deleteDeductibleExpensesByOrderId(statement.getId());
        if(!CollectionUtils.isEmpty(statement.getDeductibleExpensesList())){
            statement.getDeductibleExpensesList().forEach(deductibleExpenses -> {
                deductibleExpenses.setOrderId(statement.getId());
                deductibleExpenses.setOrderType("2");
            });
            deductibleExpensesMapper.insertDeductibleExpensess(statement.getDeductibleExpensesList());
        }
        //删除物流及辅料费用对象
        logisticsExpensesMapper.deleteLogisticsExpensesByOrderId(statement.getId());
        if(!CollectionUtils.isEmpty(statement.getLogisticsExpensesList())){
            statement.getLogisticsExpensesList().forEach(logisticsExpenses -> {
                logisticsExpenses.setOrderId(statement.getId());
                logisticsExpenses.setOrderType("2");
            });
            logisticsExpensesMapper.insertLogisticsExpensess(statement.getLogisticsExpensesList());
        }
        // 删除维修、配件费用对象
        maintenanceCostMapper.deleteMaintenanceCostByOrderId(statement.getId());
        if(!CollectionUtils.isEmpty(statement.getMaintenanceCostList())){
            statement.getMaintenanceCostList().forEach(maintenanceCost -> {
                maintenanceCost.setOrderId(statement.getId());
                maintenanceCost.setOrderType("2");
            });
            maintenanceCostMapper.insertMaintenanceCosts(statement.getMaintenanceCostList());
        }
        //删除收取客户其他费用
        otherExpensesMapper.deleteOtherExpensesByOrderId(statement.getId());
        if(!CollectionUtils.isEmpty(statement.getOtherExpensesList())){
            statement.getOtherExpensesList().forEach(otherExpenses -> {
                otherExpenses.setOrderId(statement.getId());
                otherExpenses.setOrderType("2");
            });
            otherExpensesMapper.insertOtherExpensess(statement.getOtherExpensesList());
        }

        //结算机器费用
        statementExpensesMapper.deleteStatementExpensesByStatementId(statement.getId());
        if(!CollectionUtils.isEmpty(statement.getStatementExpensesList())){
            statement.getStatementExpensesList().forEach(statementExpenses -> {
                statementExpenses.setOrderId(statement.getOrderId());
                statementExpenses.setStatementId(statement.getId());
                statementExpenses.setOrderType("2");
                statementExpensesMapper.insertStatementExpenses(statementExpenses);
            });
//            statementExpensesMapper.insertStatementExpensess(statement.getStatementExpensesList());
        }

        return statementMapper.updateStatement(statement);
    }

    /**
     * 批量删除结算单
     *
     * @param ids 需要删除的结算单主键
     * @return 结果
     */
    @Override
    public int deleteStatementByIds(Long[] ids)
    {
        return statementMapper.deleteStatementByIds(ids);
    }

    /**
     * 删除结算单信息
     *
     * @param id 结算单主键
     * @return 结果
     */
    @Override
    public int deleteStatementById(Long id)
    {
        return statementMapper.deleteStatementById(id);
    }
}
